// pages/compete/detail/detail.js
import {
  fetchAuctionDetail
} from '../../../services/good/fetchGoods';
import Toast from 'tdesign-miniprogram/toast/index';

import {
  http
} from '@/utils/request';
import dayjs from 'dayjs';

Page({
  data: {
    time: 96 * 60 * 1000,
    detail: {},
    userInfo: {},
    list: [],
    visible: false,
    id: '',
    statusMap: {
      0: '待竞价',
      1: '竞价中',
      2: '竞价结束',
    },
  },
  onLoad({
    id,
    status,
    type
  }) {
    this.setData({
      type,
      id,
      status,
    });
    this.init();
  },
  async init() {
    const userInfo = wx.getStorageSync('userInfo') || {};

    const detail = await fetchAuctionDetail(this.data.id);
    const time = -dayjs().diff(
      detail.reviewStatus === 0 ? detail.auctionStartTime : detail.auctionEndTime,
      'milliseconds',
    );
    const auctionGradient = parseInt(detail.auctionGradient);
    //const price = parseInt(detail.auctionList?.[0]?.auctionPrice || detail.startingBid);

    this.setData({
      detail,
      time,
      userInfo,
      auctionGradient,
      initPrice: detail.auctionList && detail.auctionList.length > 0 ? (detail.auctionList[0].auctionPrice + auctionGradient) : detail.startingBid,
      auctionPrice: detail.auctionList && detail.auctionList.length > 0 ? (detail.auctionList[0].auctionPrice + auctionGradient) : detail.startingBid,
      //min: Math.round(+price / auctionGradient) * auctionGradient,
      isGrandAuction: detail.goodsUserRelate.split(',').map(Number).includes(userInfo.id),
    });
  },
  onJoin() {
    this.setData({
      visible: true,
    });
  },
  async onConfirm() {
    const {
      auctionPrice,
      detail,
      initPrice
    } = this.data;
    const auctionGradient = parseInt(detail.auctionGradient);
    console.log(auctionPrice, initPrice, auctionPrice - detail.startingBid, auctionGradient);
    //如果当前无竞价记录，则可以按起拍价完成竞拍，否则要按最高竞价+阶梯价格完成竞拍
    if (
      (detail.auctionList && detail.auctionList.length > 0 && auctionPrice <= initPrice) ||
      ((!detail.auctionList || detail.auctionList.length === 0) && auctionPrice < initPrice) ||
      (auctionPrice - detail.startingBid) % auctionGradient !== 0
    ) {
      Toast({
        context: this,
        selector: '#t-toast',
        message: '金额无效,请按竞价阶梯出价',
        theme: 'error',
        direction: 'column',
      });
      return;
    }

    await http('POST', '/auction/add', {
      auctionPrice,
      goodsId: detail.id,
      goodsName: detail.name,
    });

    Toast({
      context: this,
      selector: '#t-toast',
      message: '竞价成功',
      theme: 'success',
      direction: 'column',
    });
    this.setData({
      visible: false,
    });
    this.init();
  },
  closeDialog() {
    this.setData({
      visible: false,
      auctionPrice: this.data.initPrice,
    });
  },
  goGoodsDetail() {
    wx.setStorageSync('isEdit', true);
    wx.setStorageSync('isView', true);
    wx.setStorageSync('info', 'goods');
    wx.setStorageSync('goodsId', this.data.id);
    wx.navigateTo({
      url: '/pages/compete/goods/goods?type=view&info=goods',
    });
  },
  goTradeInfo() {
    wx.setStorageSync('isEdit', true);
    wx.setStorageSync('isView', true);
    wx.setStorageSync('info', 'trade');
    wx.setStorageSync('goodsId', this.data.id);
    wx.navigateTo({
      url: '/pages/compete/goods/goods?type=view&info=trade',
    });
  },
  handleChange(e) {
    const {
      value
    } = e.detail;
    console.log('e', value);
    this.setData({
      auctionPrice: value,
    });
  },
});